import { Http, baseUrl, config } from './index';
import { useToken } from '@/hooks/useToken';
import HTTP_WHITE_LIST from './http.whiteList';
import store from '@/store';
import global from '@/global';



const REQUEST_NAME = 'request';



class HttpIntercepetor extends Http implements IHttpIntercepetor {
	constructor() {
		super(baseUrl.value, config);
	};
	init() {
		uni.addInterceptor(REQUEST_NAME, {
			invoke: (RConfig) => {
				return this.request(RConfig);
			},
			success: (result) => {
				this.successPesponse(result);
			},
			fail: (result) => {
				return this.failResponse(result);
			},
			complete: (result) => {
				// return this.response(result);
			}
		})
	};
	showLoading() {
		if (this.config.showLoading && !this.config.timer) {
			this.config.timer = setTimeout(() => {
				uni.showLoading({
					title: config.loadingText,
					mask: config.loadingMask,
				})
				this.config.timer = undefined;
			}, config.loadingTime)
		}
	};
	hideLoading() {
		uni.hideLoading();
		clearTimeout(this.config.timer);
		this.config.timer = undefined;
	};
	request(RConfig : UniNamespace.RequestOptions) {
		console.log(RConfig)
		const resturl = RConfig.url;
		if (RConfig.url.indexOf("http") == -1) {
			console.log(baseUrl.value)
			RConfig.url = `${baseUrl.value}${RConfig.url}`
		} else {
			return RConfig
		}

		this.showLoading();
		const { getTokenSync } = useToken();
		const token = getTokenSync();
		if (HTTP_WHITE_LIST.indexOf(resturl) == -1) {
			if (token) {
				RConfig.header = {
					Authorization: 'Bearer' + token
				}
			} else {
				this.hideLoading();
				global.showModel();
				return false
			}
		}
		return RConfig
	};
	response(res : UniNamespace.GeneralCallbackResult) {

	};


	successPesponse(res : UniNamespace.RequestSuccessCallbackResult) {
		console.log(res)
		this.hideLoading();
		const data = (res.data as requestData);
		if (res.statusCode !== 200) {
			uni.prompt.$showToast('请求错误，请稍后重试')
			const temp:requestData = {
				msg:'请求错误',
				status:500,
				data:{},
			}
			res.data = temp;
			res.isSuccess = false;
		} else
		if (data.status == 401 || data.status == 402 || data.status == 403 || data.status == 404) {
			const { removeTokenSync } = useToken();
			removeTokenSync();
			uni.prompt.$showToast('身份令牌过期,请重新登录');
			uni.navigateTo({
				url: '/pages_login/login_entry/loginEntry',
			})
			// return Promise.reject(res.data);
			res.isSuccess = false;
		} else if (data.code && data.code != 200) {
			uni.prompt.$showToast(data.msg);
			// return Promise.reject(res.data);
			res.isSuccess = false;
		} else if (data.status && (data.status !='success'&&data.status != 200)) {
			uni.prompt.$showToast(data.msg);
			// return Promise.reject(res.data);
			res.isSuccess = false;
		} else {
			res.isSuccess = true;
		}
		// return Promise.resolve(data);
	};
	failResponse(result : UniNamespace.GeneralCallbackResult) {
		console.log(result)
		uni.prompt.$showToast('请求错误，请稍后重试');
		// return Promise.reject(result);
	};
}

export default HttpIntercepetor